package com.lw.service.impl;

import com.lw.common.utils.StrUtils;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.lw.entity.TbConfig;
import com.lw.mapper.TbConfigMapper;
import com.lw.service.TbConfigService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.lw.entity.table.TbConfigTableDef.TB_CONFIG;

/**
 * 系统配置表 服务层实现。
 *
 * @author lw
 * @since 2025年05月28日 00:20:25
 */
@Service
public class TbConfigServiceImpl extends ServiceImpl<TbConfigMapper, TbConfig> implements TbConfigService {

    @Override
    public Page<TbConfig> getConfigPage(TbConfig tbConfig) {
        Page<TbConfig> page = new Page<>();
        page.setPageNumber(tbConfig.getPageNum());
        page.setPageSize(tbConfig.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        String keywords = tbConfig.getKeywords();
        if (StrUtils.hasLength(keywords)) {
            queryWrapper.where(TB_CONFIG.CONFIG_NAME.like(keywords).or(TB_CONFIG.CONFIG_KEY.like(keywords)));
        }
        queryWrapper.orderBy(TB_CONFIG.CREATE_TIME.desc());
        return page(page, queryWrapper);
    }

    @CacheEvict(value = "configCache",key = "'configMap'")
    @Override
    public Boolean saveConfig(TbConfig tbConfig) {
        if (StrUtils.isEmpty(tbConfig.getConfigKey())) {
            throw new RuntimeException("配置键不能为空");
        }
        if (StrUtils.isEmpty(tbConfig.getConfigName())) {
            throw new RuntimeException("配置名称为空");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.where(TB_CONFIG.CONFIG_KEY.eq(tbConfig.getConfigKey()));
        long count = count(queryWrapper);
        if (count > 0) {
            throw new RuntimeException("配置键已存在");
        }
        return save(tbConfig);
    }

    @CacheEvict(value = "configCache",key = "'configMap'")
    @Override
    public Boolean updateConfig(TbConfig tbConfig) {
        if (StrUtils.isEmpty(tbConfig.getConfigKey())) {
            throw new RuntimeException("配置键不能为空");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(TB_CONFIG.CONFIG_KEY.getName(), tbConfig.getConfigKey());
        queryWrapper.ne(TB_CONFIG.ID.getName(), tbConfig.getId());
        long count = count(queryWrapper);
        if (count > 0) {
            String errorInfo = String.format("配置键:%s已存在", tbConfig.getConfigKey());
            throw new RuntimeException("配置键:" + errorInfo + "已存在");
        }
        return updateById(tbConfig);
    }

    @CacheEvict(value = "configCache",key = "'configMap'")
    @Override
    public Boolean deleteConfig(Long id) {
        return removeById(id);
    }

    //进行缓存
    @Cacheable(value = "configCache",key = "'configMap'")
    public Map<String,Object> getConfigMap(){
        QueryWrapper queryWrapper = new QueryWrapper();
        List<TbConfig> tbConfigs = list(queryWrapper);
        Map<String,Object> configMap = new HashMap<>();
        for (TbConfig tbConfig : tbConfigs) {
            configMap.put(tbConfig.getConfigKey(),tbConfig.getConfigValue());
        }
        return configMap;
    }

    @Override
    public Object getConfigValue(String configKey) {
        //从缓存中获取配置信息
        Map<String, Object> configMap = this.getConfigMap();
        return configMap.get(configKey);
    }
}
